import {ajax} from './Promise_AJAX.js'
function Fn() {
    let uname =document.getElementById('uname'),
        tel =document.getElementById('tel'),
        ucard =document.getElementById('ucard'),
        nameParrent = /^[\u4e00-\u9fa5]{0,}$/,
        telPattern=/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/,
        ucardParrent=/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/
    function checkName() {
        if (nameParrent.test(uname.value) && (uname.value!=''))  {
            document.querySelector('span.name').innerHTML=''
            return true
        } else {
            uname.placeholder=''
            document.querySelector('span.name').innerHTML='请输入中文姓名'
            uname.value=''
            return false
        }  
    }
    function checkTel() {
        if (telPattern.test(tel.value) ) {
            document.querySelector('span.tel').innerHTML=''
            return true
        } else {
            document.querySelector('span.tel').innerHTML='请输入正确的手机号码'
            tel.value=''
        }  
    }
    function checkucard() {
        if (ucardParrent.test(ucard.value)) {
            document.querySelector('span.ucard').innerHTML=''
            return true
        } else {
            ucard.placeholder=''
            document.querySelector('span.ucard').innerHTML='请输入正确的身份证号码'
            ucard.value=''
            return false
        }  
    }
    function Appointment() {
    let  data = { "name": uname.value, "phone":tel.value, "card": ucard.value}
        ;(async ()=>{
            let getToken =await ajax(`http://newshopapi.0melon0.cn/api/f_user/register`, data, "POST")
            document.cookie=`name=${data.name}`
            document.cookie=`token=${getToken.result.token}`
            document.cookie= `phone=${data.phone}`
            location.href=`./appointment.html`
        })()  
    }
    function myAppointment() {
    let  data = { "name": uname.value, "phone":tel.value, "card": ucard.value}
        ;(async ()=>{
            let getToken =await ajax(`http://newshopapi.0melon0.cn/api/f_user/register`, data, "POST")
            document.cookie=`name=${data.name}`
            document.cookie=`token=${getToken.result.token}`
            document.cookie= `phone=${data.phone}`
            location.href=`./myAppointment.html`
        })()
     }
    uname.addEventListener('change',checkName)
    tel.addEventListener('change',checkTel) 
    ucard.addEventListener('change',checkucard)
    document.querySelector('.appointment').addEventListener('click',()=>{
        if(uname.value==''&&tel.value==''&&ucard.value==''){
            alert('请登录！')
        }else{
            checkName()&&checkTel()&&checkucard()&&Appointment()
        }
    })
    document.querySelector('.myAppointment').addEventListener('click',()=>{
        if(uname.value==''&&tel.value==''&&ucard.value==''){
            alert('请登录！')
        }else{
            checkName()&&checkTel()&&checkucard()&&myAppointment()
        }
    })
}
Fn()
// export {Fn}